/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.drawee;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.ShowcaseApplication;
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.facebook.imagepipeline.request.ImageRequest;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Switch;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class MultiUriAbility extends Ability {
    private SimpleDraweeView mSimpleDraweeView;
    private Uri mUri;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_layout_drawee_multi_uri);

        mSimpleDraweeView = (SimpleDraweeView) findComponentById(ResourceTable.Id_drawee_view);
        Button mBtnFirst = (Button) findComponentById(ResourceTable.Id_btn_first_available);
        Button mBtnIncreasing = (Button) findComponentById(ResourceTable.Id_btn_increasing_quality);
        Button mBtnBoth = (Button) findComponentById(ResourceTable.Id_btn_both);
        Switch mSwitch = (Switch) findComponentById(ResourceTable.Id_switch_use_vito);


        mBtnFirst.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                resetImageView();
                if (mSwitch.isChecked()){

                }else {
                    List<Uri> sampleUriSet = ShowcaseApplication.imageUriProvider.createSampleUriSet(null, null);
                    ImageRequest[] requests = new ImageRequest[sampleUriSet.size()];
                    int index = 0;
                    for (Uri uri : sampleUriSet){
                        requests[index] = ImageRequest.fromUri(uri);
                        index++;
                    }
                    mSimpleDraweeView.setController(
                            Fresco.newDraweeControllerBuilder()
                                    .setFirstAvailableImageRequests(requests)
                                    .build());
                }
            }
        });
        mBtnIncreasing.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                resetImageView();
                if (mSwitch.isChecked()){

                }else {
                    ImageRequest lowRes = ImageRequest.fromUri(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.XS,null,null));
                    ImageRequest highRes = ImageRequest.fromUri(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.XXL,null,null));
                    mSimpleDraweeView.setController(
                            Fresco.newDraweeControllerBuilder()
                                    .setLowResImageRequest(lowRes)
                                    .setImageRequest(highRes)
                                    .build()
                    );
                }
            }
        });
        mBtnBoth.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mSwitch.isChecked()){

                }else {
                    ImageRequest lowRes = ImageRequest.fromUri(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.XS,null,null));
                    List<Uri> sampleUriSet = new ArrayList<>();
                    sampleUriSet.add(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.L,null,null));
                    sampleUriSet.add(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.XL,null,null));
                    sampleUriSet.add(ImageUriProvider.createSampleUri(ImageUriProvider.ImageSize.XXL,null,null));
                    ImageRequest[] anyHighRes = new ImageRequest[sampleUriSet.size()];
                    int index = 0;
                    for (Uri uri : sampleUriSet){
                        anyHighRes[index] = ImageRequest.fromUri(uri);
                        index++;
                    }
                    mSimpleDraweeView.setController(
                            Fresco.newDraweeControllerBuilder()
                                    .setLowResImageRequest(lowRes)
                                    .setFirstAvailableImageRequests(anyHighRes)
                                    .build()
                    );
                }
            }
        });
    }

    private void resetImageView(){
        mSimpleDraweeView.setController(null);

    }
}
